import * as types from './mutation-types'
import { playMode } from 'common/js/config'
import { shuffle } from 'common/js/utils'
import { clearSearch, saveSearch, deleteSearch, savePlay, saveFavoriteSong, deleteFavoriteSong } from 'common/js/cache'

function findIndex (list, song) {
  return list.findIndex(item => {
    return item.id === song.id
  })
}

export const selectPlay = ({ commit, state }, { list, index }) => {
  commit(types.SET_SEQUENCE_LIST, list)
  if (state.mode === playMode.random) {
    let randomList = shuffle(list)
    commit(types.SET_PLAYLIST, randomList)
    index = findIndex(randomList, list[index])
  } else {
    commit(types.SET_PLAYLIST, list)
  }
  commit(types.SET_CURRENT_INDEX, index)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

export const randomPlay = ({ commit }, { list }) => {
  commit(types.SET_PLAY_MODE, playMode.random)
  commit(types.SET_SEQUENCE_LIST, list)
  let randomList = shuffle(list)
  commit(types.SET_PLAYLIST, randomList)
  commit(types.SET_CURRENT_INDEX, 0)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

export const insertSong = function ({ commit, state }, song) {
  let playlist = state.playlist.slice() // slice创建副本
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  let currentSong = playlist[currentIndex]
  const fpIndex = findIndex(playlist, song)
  currentIndex++
  playlist.splice(currentIndex, 0, song)
  // 如果歌曲在播放列表中
  if (fpIndex !== -1) {
    // 歌曲在插入位置以前
    if (fpIndex < currentIndex) {
      playlist.splice(fpIndex, 1)
      currentIndex-- // 因为已经删除了元素
    } else {
      // 歌曲在插入位置之后， 因为插入了歌曲，所有index+1了
      playlist.splice(fpIndex + 1, 1)
    }
  }
  let currentSIndex = findIndex(sequenceList, currentSong) + 1 // 顺序列表在当前歌曲之后插入新歌
  let fsIndex = findIndex(sequenceList, song)
  sequenceList.splice(currentSIndex, 0, song)
  if (fsIndex !== -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
    } else {
      sequenceList.splice(fsIndex + 1, 1)
    }
  }
  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

export const saveSearchHistory = function ({ commit }, query) {
  let history = saveSearch(query)
  commit(types.SET_SEARCH_HISTORY, history)
}

export const deleteSearchHistory = function ({ commit }, item) {
  let newHis = deleteSearch(item)
  commit(types.SET_SEARCH_HISTORY, newHis)
}

export const clearSearchHistory = function ({ commit }) {
  clearSearch()
  commit(types.SET_SEARCH_HISTORY, [])
}

export const deleteSong = function ({ commit, state }, item) {
  let sequenceList = state.sequenceList.slice()
  let playlist = state.playlist.slice()
  let currentIndex = state.currentIndex
  let sIndex = findIndex(sequenceList, item)
  sequenceList.splice(sIndex, 1)
  let pIndex = findIndex(playlist, item)
  playlist.splice(pIndex, 1)
  // 为了防止currrentSong切换  如果当前是最后一首歌
  if (pIndex < currentIndex || currentIndex === playlist.length) {
    currentIndex--
  }
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  if (!playlist.length) {
    commit(types.SET_PLAYING_STATE, false)
  } else {
    commit(types.SET_PLAYING_STATE, true)
  }
}

export const clearList = function ({commit, state}) {
  if (state.playing) {
    commit(types.SET_PLAYING_STATE, false)
  }
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
}

export const savePlayHistory = function ({commit, state}, song) {
  commit(types.SET_PLAY_HISTORY, savePlay(song))
}

export const saveFavorite = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, saveFavoriteSong(song))
}

export const deleteFavorite = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, deleteFavoriteSong(song))
}
